#include "buttonspanel.h"
#include <QPainter>
#include <QMatrix>
#include <QLayout>
#include <QtDebug>

using namespace ExecTerminalImplNS;

const int COMBOBOX_ADDITIONAL_WIDTH = 80;

ButtonsPanel::ButtonsPanel(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);
    int minWidth = 0;
    const QList<QPushButton *> & pblist = findChildren<QPushButton *>();
    foreach (QPushButton * btn, pblist)
    {
        minWidth += btn->width();
    }
    minWidth += addressComboBox()->sizeHint().width() + COMBOBOX_ADDITIONAL_WIDTH;
    setMinimumSize(QSize(minWidth, layout()->minimumSize().height()));
}

ButtonsPanel::~ButtonsPanel()
{

}

void ButtonsPanel::switchVLayout()
{
    if (layout() == 0)
    {
        return;
    }
    
    QLayoutItem * itm = layout()->itemAt(0);
    setUpdatesEnabled(false);
    layout()->removeItem(itm);
    layout()->addItem(itm);
    layout()->activate();
    setUpdatesEnabled(true);
}

void ButtonsPanel::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if (!isUpOrientation())
    {
        QMatrix rotMatrix;
        rotMatrix.translate(width(), height());
        rotMatrix.rotate(180);
        painter.setMatrix(rotMatrix);
    }
    static QPixmap backgrnd (":/Resources/top_bg_gradient_1.png");
    painter.drawTiledPixmap(0, 0, width(), height(), backgrnd);
}

bool ButtonsPanel::isUpOrientation()const
{
    if (layout() == 0)
    {
        return true;
    }
    return layout()->itemAt(0)->spacerItem() != 0;
}


FlexibleComboBox * ButtonsPanel::addressComboBox()const
{
    return findChild<FlexibleComboBox*>();
}

